package yx.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;

import yx.service.InvitationServiceInt;
import yx.vo.InvitationReplyVO;
import yx.vo.InvitationVO;
import yx.vo.JsonVO;
import yx.vo.UserVO;

import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("basePackage")
@Namespace("/")
@Action(value = "invitationAction")
public class InvitationAction extends BaseAction implements ModelDriven<InvitationVO>, RequestAware, SessionAware {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(InvitationAction.class);

	private InvitationVO invitation = new InvitationVO();

	@Override
	public InvitationVO getModel() {
		return invitation;
	}

	private Map<String, Object> request;
	private Map<String, Object> session;

	public Map<String, Object> getRequest() {
		return request;
	}

	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	private InvitationServiceInt invitationService;

	public InvitationServiceInt getInvitationService() {
		return invitationService;
	}

	@Autowired
	public void setInvitationService(InvitationServiceInt invitationService) {
		this.invitationService = invitationService;
	}

	public void queryInvitation() {
		super.writeJson(invitationService.datagrid(invitation));
	}
	
	public void initInvitation() {
		HttpSession httpSession = ServletActionContext.getRequest().getSession();
		UserVO user = (UserVO) httpSession.getAttribute("LOGIN_USER");
		// 热帖
		List<InvitationVO> list1 = invitationService.queryHotInvitation();
		session.put("KEY_HOT_INVITATION", list1);
		// 新帖
		List<InvitationVO> list2 = invitationService.queryNewInvitation();
		session.put("KEY_NEW_INVITATION", list2);
		// 我参与的帖子
		List<InvitationVO> list3 = invitationService.queryMyInvitation(user);
		session.put("KEY_MY_INVITATION", list3);
		
		Map<String, Object> map = invitationService.queryInvitationSat();
		session.put("KEY_INVITATION_SAT", map);
		super.writeJson(map);
	}
	
	public void queryInvitationList() {
		invitation.setForumZone(Integer.valueOf(invitation.getForumZoneId()));
		super.writeJson(invitationService.queryInvitatioList(invitation));
	}

	public void queryInvitationAndReplyList() {
		InvitationVO invo = invitationService.queryInvitationAndReplyList(invitation);
		List<InvitationReplyVO> retList = invo.getReplyInvitations();
		session.put("KEY_INVITATION", invo);
		session.put("KEY_INVITATION_REPLY", retList);
		super.writeJson(invo);
	}

	public void addInvitation() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		UserVO user = (UserVO) session.getAttribute("LOGIN_USER");
		invitation.setCreatorId(user.getId());
		invitation.setCreatorName(user.getRealName());
		invitation.setUpdaterName(user.getRealName());
		JsonVO j = new JsonVO();
		try {
			invitation.setForumZone(Integer.valueOf(invitation.getForumZoneId()));
			InvitationVO u = invitationService.save(invitation);
			j.setSuccess(true);
			j.setMsg("增加成功！");
			j.setObj(u);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}

	public void removeInvitation() {
		invitationService.remove(invitation.getIds());
		JsonVO j = new JsonVO();
		j.setSuccess(true);
		j.setMsg("删除成功！");
		super.writeJson(j);
	}

	public void editInvitation() {
		InvitationVO u = invitationService.edit(invitation);
		JsonVO j = new JsonVO();
		j.setSuccess(true);
		j.setMsg("编辑成功！");
		j.setObj(u);
		super.writeJson(j);
	}

}
